﻿@page "/Credentials"
@inject ISnackbar Snackbar
@using HardHatCore.HardHatC2Client.Models
@using HardHatCore.HardHatC2Client.Services;
@using HardHatCore.HardHatC2Client.Components;


<CascadingAuthenticationState>
    <AuthorizeView Roles="Operator,TeamLead">
        <Authorized>
            <h3 class="text-center">Credentials</h3>

            <div class="container-fluid" style="height:500px; overflow-y:scroll">
                <div class="row">
                    <div class="col-md-12">
                        <MudTable Items="@Creds" Dense="true" Hover="true" ReadOnly="false" CanCancelEdit="true" HorizontalScrollbar="true" FixedHeader="true" Height="400px"  Elevation="10" Outlined="true" Striped="true"
                                  @bind-SelectedItem="selectedItem1" CommitEditTooltip="Commit Edit"
                                  OnCommitEditClick="@(() => Snackbar.Add("Commit Edit Handler Invoked"))" RowEditPreview="BackupItem" RowEditCancel="ResetItemToOriginalValues"
                                  RowEditCommit="ItemHasBeenCommitted" IsEditRowSwitchingBlocked="true">
                            <HeaderContent>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Name</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Domain</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Credential</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Type</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">SubType</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Capture Time</MudTh>
                            </HeaderContent>
                            <RowTemplate Context="Rowcontext">
                                <MudTd DataLabel="Name">@Rowcontext.Name</MudTd>
                                <MudTd DataLabel="Domain">@Rowcontext.Domain</MudTd>
                                <MudTd DataLabel="Password">@Rowcontext.CredentialValue</MudTd>
                                <MudTd DataLabel="Cred Type">@Rowcontext.Type</MudTd>
                                <MudTd DataLabel="Cred Type">@Rowcontext.SubType</MudTd>
                                <MudTd DataLabel="Capture Time">@Rowcontext.CaptureTime</MudTd>
                            </RowTemplate>
                            <RowEditingTemplate Context="Rowcontext">
                                <MudTd  DataLabel="Name">
                                    <MudTextField @bind-Value="@Rowcontext.Name" Required />
                                </MudTd>
                                <MudTd DataLabel="Domain">
                                    <MudTextField @bind-Value="@Rowcontext.Domain" Required />
                                </MudTd>
                                <MudTd DataLabel="Credential">
                                    <MudTextField @bind-Value="@Rowcontext.CredentialValue" Required />
                                </MudTd>
                                <MudTd DataLabel="Type">
                                    <MudTextField @bind-Value="@Rowcontext.Type" Required/>
                                </MudTd>
                                <MudTd DataLabel="SubType">
                                    <MudTextField @bind-Value="@Rowcontext.SubType" Required/>
                                </MudTd>
                            </RowEditingTemplate>
                        </MudTable>
                    </div>
                </div>
            </div>

            <br>
            <div class ="container-fluid">
                <h3 class="text-center position-relative">Submit Creds</h3>
                <hr>

                <MudForm @bind-IsValid="@success" @bind-Errors="@errors">
                    <MudTextField Class="mb-4 mt-4"  T="string" Label="Username" Variant="Variant.Filled" Required="true" @bind-value="formData.Name" RequiredError="username is required!"/>
                    <MudTextField Class="mb-4 mt-4" T="string" Label="Domain" Variant="Variant.Filled"  Required="false" @bind-value="formData.Domain"/>
                    <MudTextField Class="mb-4 mt-4" T="string" Label="Credential Value" Variant="Variant.Filled"  Required="true" @bind-value="formData.CredentialValue" RequiredError="cred value is required!"/>
                    <MudSelect Class="mb-4 mt-4"  T="Cred.CredType" Label="Cred Type" Variant="Variant.Filled" Required="true" @bind-value="formData.Type" RequiredError="Type is required!">
                        <MudSelectItem Value="@(Cred.CredType.password)" />
                        <MudSelectItem Value="@(Cred.CredType.hash)" />
                        <MudSelectItem Value="@(Cred.CredType.ticket)" /> 
                    </MudSelect>
                    <MudButton StartIcon="@Icons.Filled.Bolt" Variant="Variant.Filled" Color="Color.Success"  Disabled="@(!success)" @onclick="@validSubmit">Create</MudButton>
                </MudForm>
            </div>

        </Authorized>
    </AuthorizeView>
    <AuthorizeView Roles="Guest">
        <Authorized>
            <h3 class="text-center">Credentials</h3>
    
            <div class="container-fluid" style="height:500px; overflow-y:scroll">
                <div class="row">
                    <div class="col-md-12">
                        <MudTable Items="@Creds" Dense="true" Hover="true" ReadOnly="false" CanCancelEdit="true" HorizontalScrollbar="true" FixedHeader="true" Height="400px"  Elevation="10" Outlined="true" Striped="true"
                                   CommitEditTooltip="Commit Edit"
                                  IsEditRowSwitchingBlocked="true">
                            <HeaderContent>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Name</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Domain</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Credential</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Type</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">SubType</MudTh>
                                <MudTh Style="@setStyle(Settings.IsCheckedBox)">Capture Time</MudTh>
                            </HeaderContent>
                            <RowTemplate Context="Rowcontext">
                                <MudTd DataLabel="Name">@Rowcontext.Name</MudTd>
                                <MudTd DataLabel="Domain">@Rowcontext.Domain</MudTd>
                                <MudTd DataLabel="Password">@Rowcontext.CredentialValue</MudTd>
                                <MudTd DataLabel="Cred Type">@Rowcontext.Type</MudTd>
                                <MudTd DataLabel="Cred Type">@Rowcontext.SubType</MudTd>
                                <MudTd DataLabel="Capture Time">@Rowcontext.CaptureTime</MudTd>
                            </RowTemplate>
                        </MudTable>
                    </div>
                </div>
            </div>
    
            <br>
        </Authorized>
    </AuthorizeView>
     <AuthorizeView Roles="TeamLead,Operator,Guest">
                   <NotAuthorized>
                        <h1 class="text-center">Not Authorized</h1>
                        <UnAuthorizedDialogBox></UnAuthorizedDialogBox>
                    </NotAuthorized>
        </AuthorizeView>
</CascadingAuthenticationState>

@code {
    private List<string> editEvents = new();
    private Cred selectedItem1 = null;
    private Cred CredBeforeEdit;
    private Cred formData = new Cred();
    private HashSet<Cred> selectedItems1 = new HashSet<Cred>();

    private static List<Cred> Creds = new List<Cred>();
    private bool success;
    private string[] errors = { };


    private string setStyle(bool IsDark)
    {
        if(IsDark)
        {
            return "background:white; color:black;";
        }
        return "background:#333333; color:white;"; 
    }

    public async Task validSubmit()
    {
        Creds.Add(formData);
        await HardHatHubClient._hub.AddCred(formData);
        formData = new();
    }

    private void AddEditionEvent(string message)
    {
        editEvents.Add(message);
        StateHasChanged();
    }

    public static async Task AddCreds(List<Cred> creds)
    {
        foreach (Cred cred in creds)
        {
            Creds.Add(cred);
        }
    }

    private void BackupItem(object Cred)
    {
        CredBeforeEdit = new()
        {
            Name = ((Cred)Cred).Name,
            Domain = ((Cred)Cred).Domain,
            CredentialValue = ((Cred)Cred).CredentialValue,
            Type = ((Cred)Cred).Type,
            SubType = ((Cred)Cred).SubType
        };
        AddEditionEvent($"RowEditPreview event: made a backup of Cred {((Cred)Cred).Name}");
    }

    private void ItemHasBeenCommitted(object Cred)
    {
        AddEditionEvent($"RowEditCommit event: Changes to Cred {((Cred)Cred).Name} committed");
    }

    private void ResetItemToOriginalValues(object Cred)
    {
        ((Cred)Cred).Name = CredBeforeEdit.Name;
        ((Cred)Cred).Domain = CredBeforeEdit.Domain;
        ((Cred)Cred).CredentialValue = CredBeforeEdit.CredentialValue;
        ((Cred)Cred).Type = CredBeforeEdit.Type;
        ((Cred)Cred).SubType = CredBeforeEdit.SubType;
        AddEditionEvent($"RowEditCancel event: Editing of Cred {((Cred)Cred).Name} cancelled");
    }
}
